/*
 * Copyright (c) 2009-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/gfx/IGraphics2DDevice.h"
#include "alkes/gfx/RenderTarget.h"

namespace alkes {

IGraphics2DDevice::IGraphics2DDevice()
{
}

IGraphics2DDevice::~IGraphics2DDevice()
{
}

void IGraphics2DDevice::snapshot(ImageWriter& writer, const Rect* rect)
{
    Rect target_rect(getTargetRect());
    Rect tmp(!rect ? target_rect : *rect);

    if (!rect)
        tmp.intersectWith(target_rect);

    AL_PRE_COND(!tmp.isNull(), );

    onSnapshot(writer, tmp);
}

void IGraphics2DDevice::setAttribute(int attr, bool is_on)
{
    setFlag(attr, is_on);
}

bool IGraphics2DDevice::correctTexelMapping() const
{
    return this->flag(CORRECT_TEXEL_MAPPING);
}

Shape* IGraphics2DDevice::createShape(int32_t shape_type, uint32_t num)
{
    Shape* shape = 0;
    switch (shape_type)
    {
    case AL_SHAPE_LINE:
        shape = createLineShape(num);
        break;
    case AL_SHAPE_RECTANGLE:
        shape = createRectangleShape(num);
        break;
    case AL_SHAPE_IMAGE:
        shape = createImageShape(num);
        break;
    default:
        break;
    }
    return shape;
}

LineShape* IGraphics2DDevice::createLineShape(uint32_t num)
{
    (void)num;

    return 0;
}

RectangleShape* IGraphics2DDevice::createRectangleShape(uint32_t num)
{
    (void)num;

    return 0;
}

ImageShape* IGraphics2DDevice::createImageShape(uint32_t num)
{
    (void)num;

    return 0;
}

PolylineShape* IGraphics2DDevice::createPolylineShape(uint32_t num)
{
    (void)num;

    return 0;
}

ImageShape* IGraphics2DDevice::createImageShape(uint32_t num, ImageBuffer* image, const Point2D* dst_xys)
{
    if (!image)
        return 0;

    ImageShape* shape = createImageShape(num);
    if (!shape)
        return 0;

    shape->setImage(image);

    if (dst_xys)
    {
        uint32_t i;
        for (i = 0; i < num; ++i)
            shape->setParam(i, dst_xys[i]);
    }

    return shape;
}

ImageShape* IGraphics2DDevice::createImageShape(ImageBuffer* image, const Point2D& dst_xy)
{
    return createImageShape(1, image, &dst_xy);
}

ImageShape* IGraphics2DDevice::createImageShape(uint32_t num, ImageBuffer* grayscale_image, const Point2D* dst_xys, const Color& color)
{
    if (!grayscale_image)
        return 0;

    ImageShape* shape = createImageShape(num);
    if (!shape)
        return 0;

    shape->setImage(grayscale_image);

    uint32_t i;
    for (i = 0; i < num; ++i)
    {
        if (dst_xys)
            shape->setParam(i, dst_xys[i], color);
        else
            shape->setParam(i, Point2D::ZERO, color);
    }

    return shape;
}

ImageShape* IGraphics2DDevice::createImageShape(ImageBuffer* grayscale_image, const Point2D& dst_xy, const Color& color)
{
    return createImageShape(1, grayscale_image, &dst_xy, color);
}

void IGraphics2DDevice::fillAll(const Color& color)
{
    setScissorRect(getTargetRect());
    fill(color);
}

bool IGraphics2DDevice::drawShape(Shape* shape, uint32_t start, uint32_t count)
{
    if (!shape || !count)
    {
        return false;
    }

    bool ret = false;

    if (shape->isInstanceOf(ImageShape::classID()))
    {
        ret = drawShape(static_cast<ImageShape*>(shape), start, count);
    }
    else if (shape->isInstanceOf(RectangleShape::classID()))
    {
        ret = drawShape(static_cast<RectangleShape*>(shape), start, count);
    }
    else if (shape->isInstanceOf(LineShape::classID()))
    {
        ret = drawShape(static_cast<LineShape*>(shape), start, count);
    }
    else if (shape->isInstanceOf(PolylineShape::classID()))
    {
        ret = drawShape(static_cast<PolylineShape*>(shape), start, count);
    }

    return ret;
}

void IGraphics2DDevice::translate(const Vector2D& offset)
{
    translate(offset.x, offset.y);
}

void IGraphics2DDevice::translateForImage(float x, float y)
{
    translate(x, y);
}

void IGraphics2DDevice::scale(float scaling, const Vector2D& origin)
{
    scale(scaling, scaling, origin);
}

}
